<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use think\facade\Env;
use think\Request;
class Common extends Controller
{
	// protected $beforeActionList=[
	// 'text'
	// ];
    public function layout()
    {
        
        return $this->fetch();

    }
    // 参考文档  https://www.kancloud.cn/manual/thinkphp5_1/354121
    public function upaction($file){
    	// return Env::get('root_path');
	     // foreach($files as $file){
		    //上传回调error为0
		    
		    if(empty($file)){
		        $result["error"] = "1";
		        $result['data'] = '';
		    }else{
		        // 移动到框架应用根目录/public/uploads/ 目录下
		        $rpath=Env::get('root_path');
		        $info = $file->move($rpath . 'public/static/uploads' );
		        if($info){
		            $name_path =$info->getSaveName();
		            //成功上传后 获取上传信息
		            $name_path =str_replace('\\',"/",$info->getSaveName());
		          //  $name_path =$info->getSaveName();
		            $result["error"] = '0';
		            $result['data'] = "/static/uploads/".$name_path;
		        }else{
		            // 上传失败获取错误信息
		            $result["code"] = "2";
		            $result['data'] ='';
		        }
		    }
	      exit(json_encode($result));
	      
	     // }
    }
    public function upaction1($file){
    	// return Env::get('root_path');
	     // foreach($files as $file){
		    //上传回调error为0
		    // $file=request()->param('file');
		    if(empty($file)){
		        $result["error"] = "1";
		        $result['data'] = '';
		    }else{
		        // 移动到框架应用根目录/public/uploads/ 目录下
		        $rpath=Env::get('root_path');
		        $info = $file->move($rpath . 'public/static/uploads/thumb' );
		        if($info){
		            $name_path =$info->getSaveName();
		            //成功上传后 获取上传信息
		            $name_path =str_replace('\\',"/",$info->getSaveName());
		            $result["error"] = '0';
		            $result['data'] = "/static/uploads/thumb/".$name_path;
		            return $result['data'];
		        }else{
		            // 上传失败获取错误信息
		            $result["code"] = "2";
		            $result['data'] ='';
		            return $result['data'];
		        }
		    }
	      
	     // }
    }
    // 上传生成的二维码
    public function upaction1code($file){
    	// return Env::get('root_path');
	     // foreach($files as $file){
		    //上传回调error为0
		    // $file=request()->param('file');
		    if(empty($file)){
		        $result["error"] = "1";
		        $result['data'] = '';
		    }else{
		        // 移动到框架应用根目录/public/uploads/ 目录下
		        $rpath=Env::get('root_path');
		        $info = $file->move($rpath . 'public/static/uploads/thumb' );
		        if($info){
		            $name_path =$info->getSaveName();
		            //成功上传后 获取上传信息
		            $name_path =str_replace('\\',"/",$info->getSaveName());
		            $result["error"] = '0';
		            $result['data'] = "/static/uploads/thumb/".$name_path;
		            return $result['data'];
		        }else{
		            // 上传失败获取错误信息
		            $result["code"] = "2";
		            $result['data'] ='';
		            return $result['data'];
		        }
		    }
	      
	     // }
    }
    public function upaction2($files){
    	// return Env::get('root_path');
    // 	var_dump($files);
    	$resdata=[];
	      foreach($files as $key=>$file){
		    //上传回调error为0
		  //  $file=request()->param('file');
		    if(empty($file)){
		        $result["error"] = "1";
		        $result['data'] = '';
		    }else{
		        // 移动到框架应用根目录/public/uploads/ 目录下
		        $rpath=Env::get('root_path');
		        $info = $file->move($rpath . 'public/static/uploads/banner' );
		        if($info){
		            $name_path =$info->getSaveName();
		            //成功上传后 获取上传信息
		            $name_path =str_replace('\\',"/",$info->getSaveName());
		            $result["error"] = '0';
		            $result['data'] = "/static/uploads/banner/".$name_path;
		          //  return $result['data'];
		        }else{
		            // 上传失败获取错误信息
		            $result["code"] = "2";
		            $result['data'] ='';
		          //  return $result['data'];
		        }
		    }
		    $resdata[$key]=$result['data'];
	      }
	      return $resdata;
    }
    public function upload(){
	     $file = request()->file('file');
	     $this->upaction($file);
	     
	}

}
